package org.darkbits.cyklon.util;

import java.io.Serializable;

public final class Complex implements Serializable {
	private static final long serialVersionUID = 3233163302801267671L;
	public final float re, im;

	public Complex(float re, float im) {
		this.re = re;
		this.im = im;
	}

	public Complex(float v) {
		this.re = (float) Math.cos(v);
		this.im = (float) Math.sin(v);
	}

	public Complex add(Complex that) {
		return new Complex(this.re + that.re, this.im + that.im);
	}
	
	public Complex add(float re) {
		return new Complex(this.re + re, this.im);
	}
	
	public Complex sub(Complex that) {
		return new Complex(this.re - that.re, this.im - that.im);
	}
	
	public Complex sub(float re) {
		return new Complex(this.re - re, this.im);
	}

	public Complex mult(Complex that) {
		return new Complex(this.re * that.re - this.im * that.im, this.re * that.im + this.im * that.re);
	}
	
	public Complex mult(float re) {
		return new Complex(this.re * re, this.im * re);
	}

	public Complex div(Complex that) {
		return this.mult(new Complex(that.re, -that.im)).div(that.re * that.re + that.im * that.im);
	}
	
	public Complex div(float re) {
		return new Complex(this.re / re, this.im / re);
	}
	
	public float abs() {
		return (float)Math.sqrt(re * re + im * im);
	}
	
	public float abs2() {
		return re * re + im * im;
	}
	
	public String toString() {
		if (im < 0) {
			return String.valueOf(re) + String.valueOf(im) + "i";
		} else {
			return String.valueOf(re) + "+" + String.valueOf(im) + "i";
		}
	}
}
